/**
* 版权声明：睿商(厦门)科技股份有限公司 版权所有 违者必究
* 日    期：2021-07-06
*/
package com.rzico.admin.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.admin.entity.Company;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.admin.entity.ProjectActivity;
import com.rzico.admin.service.ProjectActivityService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 项目活动记录表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-07-06
 */
@Api(description = "项目活动记录表接口")
@RestController
@RequestMapping("/admin/projectActivity")
public class ProjectActivityAdminController extends BaseController {

    @Autowired
    private ProjectActivityService projectActivityService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询项目活动记录表
     *
     * @return
    */
    @ApiOperation("分页查询项目活动记录表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "status", value = "状态(0.草稿,1.提交,2.审核,3.驳回)", dataType = "Integer", paramType = "query")
    })
    public CommResult<ProjectActivity> list(Long companyId,Integer status,Long projectId,Date startDate, Date endDate,String keyword, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        if (keyword!=null){
            params.put("keyword", keyword);
        }



        if (companyId!=null) {
            params.put("companyId", companyId);
        }
        if (status!=null){
            params.put("status", status);
        }
        if (projectId!=null) {
            params.put("projectId", projectId);
        }

        SysUser sysUser = sysUserService.getCurrent();
        if (!sysUserService.isAdmin(sysUser)) {
            params.put("dataScopeId", sysUser.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProjectActivity> list = projectActivityService.selectList(params);
        PageResult<ProjectActivity> pageResult = new PageResult<ProjectActivity>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条项目活动记录表
     *
     * @return
    */
    @ApiOperation("查询单条项目活动记录表")
    @GetMapping("/find/{id}")
    public CommResult<ProjectActivity> find(@PathVariable String id) {
        ProjectActivity result = projectActivityService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("projectActivity",result);
        return CommResult.success(data);

    }

    /**
     * 保存项目活动记录表
     *
     * @param projectActivity
     * @return
     */
    @ApiOperation("保存项目活动记录表")
    @PostMapping("/save")
    public CommResult<ProjectActivity> save(@RequestBody ProjectActivity projectActivity) {
        int affectCount = projectActivityService.insert(projectActivity);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        ProjectActivity result = projectActivityService.findById(projectActivity.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除项目活动记录表
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除项目活动记录表", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除项目活动记录表,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<ProjectActivity> del(@PathVariable String ids) {

        projectActivityService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新项目活动记录表
     *
     * @param projectActivity
     * @return
     */
    @Log(desc = "更新项目活动记录表", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新项目活动记录表")
    @PostMapping("/update")
    public CommResult<ProjectActivity> update(@RequestBody ProjectActivity projectActivity) {
        int affectCount = projectActivityService.updateByPrimaryKeySelective(projectActivity);
        if (affectCount <= 0){
            return CommResult.error();
        }
        ProjectActivity result = projectActivityService.findById(projectActivity.getId());
        return CommResult.success(result);
    }

    /**
     * 提交项目活动记录
     * @param projectActivityId
     * @return
     */
    @ApiOperation("提交项目活动记录")
    @PostMapping("/submit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectActivityId", value = "项目活动id", dataType = "Long", paramType = "query")
    })
    public CommResult<ProjectActivity> submit(Long projectActivityId) {
        int affectCount = projectActivityService.submit(projectActivityId);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }

    /**
     * 审核项目活动记录
     * @param projectActivityId
     * @return
     */
    @ApiOperation("审核项目活动记录")
    @PostMapping("/adopt")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectActivityId", value = "项目活动id", dataType = "Long", paramType = "query")
    })
    public CommResult<ProjectActivity> adopt(Long projectActivityId) {
        int affectCount = projectActivityService.adopt(projectActivityId);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }

    /**
     * 驳回项目活动记录
     * @param projectActivityId
     * @return
     */
    @ApiOperation("驳回项目活动记录")
    @PostMapping("/reject")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectActivityId", value = "项目活动id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "rejectText", value = "驳回理由", dataType = "String", paramType = "query")
    })
    public CommResult<ProjectActivity> reject(Long projectActivityId,String rejectText) {
        int affectCount = projectActivityService.reject(projectActivityId,rejectText);
        if (affectCount <= 0){
            return CommResult.error();
        }
        return CommResult.success();
    }
}
